#include<QMenu>
#include<QMenuBar>
#include<fstream>

#include "calendarwindow.h"
#include "calendar.h"

using namespace std;

CalendarWindow::CalendarWindow(QWidget *parent) : QMainWindow(parent)
{
	calendar=new Calendar(this);

	QString windowTitle="My Calendar - ";
	windowTitle.append(QDate::currentDate().toString("MMM d, yyyy"));

	setCentralWidget(calendar);
	setWindowTitle(windowTitle);

	calendarMenu=menuBar()->addMenu("Calendar");
	exitAction=calendarMenu->addAction("Exit");
	showCalendarGridAction=calendarMenu->addAction("Show Grid");
	showCalendarGridAction->setCheckable(true);
	showWeekNumbersAction=calendarMenu->addAction("Show week numbers");
	showWeekNumbersAction->setCheckable(true);


	eventsMenu=menuBar()->addMenu("Events");
	addEventAction=eventsMenu->addAction("Add Event");
	deleteEventAction=eventsMenu->addAction("Delete Event");

	tableMenu=menuBar()->addMenu("Table");
	showTableGridAction=tableMenu->addAction("Show Grid");
	showTableGridAction->setCheckable(true);

	connect(addEventAction, SIGNAL(triggered()),
			calendar, SLOT(addEvent()));

	connect(deleteEventAction, SIGNAL(triggered()),
			calendar, SLOT(deleteEvent()));

	connect(exitAction, SIGNAL(triggered()),
			this, SLOT(close()));

	connect(showCalendarGridAction, SIGNAL(triggered(bool)),
			calendar, SLOT(showCalendarGrid(bool)));

	connect(showTableGridAction, SIGNAL(triggered(bool)),
			calendar, SLOT(showTableGrid(bool)));

	connect(showWeekNumbersAction, SIGNAL(triggered(bool)),
			calendar, SLOT(showWeekNumbers(bool)));

	bool s;
	fstream settings;
	settings.open(".settings", ios_base::in);
	settings>>s;
	showCalendarGridAction->setChecked(s);
	settings>>s;
	showWeekNumbersAction->setChecked(s);
	settings>>s;
	showTableGridAction->setChecked(s);
	settings.close();
	resize(750, 300);
	setWindowIcon(QIcon("images/calendar.png"));
}

CalendarWindow::~CalendarWindow()
{
	fstream settings;
	settings.open(".settings", ios_base::out);
	settings<<showCalendarGridAction->isChecked()<<endl;
	settings<<showWeekNumbersAction->isChecked()<<endl;
	settings<<showTableGridAction->isChecked()<<endl;
	settings.close();
}
